﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Web.UI.WebControls;
using Medianamik.Core;
using Medianamik.Core.Sugar;
using Medianamik.Globalization;
using Medianamik.UI.Web.MVP.Presentation;
using Medianamik.UI.Web.MVP.View;

namespace Medianamik.UI.Web.Shop
{
    public interface IEditAttributeTypeView : IModuleView
    {
        event EventHandler OnSave;
        event EventHandler OnSelectedDataTypeChanged;
        event EventHandler OnSelectedValidationExpressionChanged;

        Guid? TypeId { get; }
        Guid? SelectedDataType { get; }
        string SelectedValidationExpression { get; }
        string AdvancedValidationExpression { get; set; }
        short? MinLength { get; set; }
        short? MaxLength { get; set; }
        bool ValuesAreLocalized { get; }
        string IconPath { get; }

        bool IsAdvancedValidation { get; }

        bool TextValidationVisible { get; set; }
        bool AdvancedValidationVisible { get; set; }

        IDictionary<string, string> TypeNameValuesByCulture { get; }
        IEnumerable<PropertyDefinition> DataTypes { set; }
        IEnumerable<PropertyValidationExpression> ValidationExpression { set; }
    }

    public partial class ShopEditAttributeType : PageModuleViewWithPresenter<ModulePresenter<IEditAttributeTypeView>>, IEditAttributeTypeView
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            ddlDataTypes.DataTextField = "LocalizedName";
            ddlDataTypes.DataValueField = "Id";

            ddlValidation.DataTextField = "LocalizedName";
            ddlValidation.DataValueField = "Pattern";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Save(object sender, EventArgs e)
        {
            if(Page.IsValid)
            {
                if(OnSave != null)
                    OnSave(this, EventArgs.Empty);
            }
        }

        public event EventHandler OnSave;
        public event EventHandler OnSelectedDataTypeChanged;
        public event EventHandler OnSelectedValidationExpressionChanged;

        public Guid? TypeId
        {
            get { return string.IsNullOrEmpty(QueryString[DefaultParamNames.TypeID]) ? (Guid?)null : new Guid(QueryString[DefaultParamNames.TypeID]); }
        }

        public Guid? SelectedDataType
        {
            get
            {
                return string.IsNullOrEmpty(ddlDataTypes.SelectedValue) ? (Guid?)null : new Guid(ddlDataTypes.SelectedValue);
            }
        }

        public string SelectedValidationExpression
        {
            get { return ddlValidation.SelectedValue; }
        }

        public string AdvancedValidationExpression
        {
            get { return txtRegex.Text; }
            set { txtRegex.Text = value; }
        }

        public short? MinLength
        {
            get { return string.IsNullOrEmpty(txtMinLength.Text) ? (short?)null : short.Parse(txtMinLength.Text); }
            set { txtMinLength.Text = value.ToString(); }
        }

        public short? MaxLength
        {
            get { return string.IsNullOrEmpty(txtMaxLength.Text) ? (short?)null : short.Parse(txtMaxLength.Text); }
            set { txtMaxLength.Text = value.ToString(); }
        }

        public bool ValuesAreLocalized
        {
            get { return cbValuesAreLocalized.Checked; }
        }

        public string IconPath
        {
            get { return IconPicker.Value; }
        }

        public bool IsAdvancedValidation
        {
            get { return SelectedValidationExpression == "advanced"; }
        }

        public bool AdvancedValidationVisible
        {
            get { return phAdvancedValidation.Visible; }
            set { phAdvancedValidation.Visible = value; }
        }

        public bool TextValidationVisible
        {
            get { return phTextValidation.Visible; }
            set { phTextValidation.Visible = value; }
        }

        public IDictionary<string, string> TypeNameValuesByCulture
        {
            get { return TypeNameTranslations.Values; }
        }

        public IEnumerable<PropertyDefinition> DataTypes
        {
            get { return (IEnumerable<PropertyDefinition>)ddlDataTypes.DataSource; }
            set
            {
                ddlDataTypes.DataSource = value;
                ddlDataTypes.DataBind();
                ddlDataTypes.Items.Insert(0, "");
            }
        }

        public IEnumerable<PropertyValidationExpression> ValidationExpression
        {
            set
            {
                ddlValidation.DataSource = value;
                ddlValidation.DataBind();
                ddlValidation.Items.Insert(0, "");
                ddlValidation.Items.Add(new ListItem(Translator.Translate("medianamik_typemanagement", "advanced"), "advanced"));
            }
        }

        protected void ddlDataTypes_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            if (OnSelectedDataTypeChanged != null)
                OnSelectedDataTypeChanged(sender, e);
        }

        protected void ddlValidation_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            if (OnSelectedValidationExpressionChanged != null)
                OnSelectedValidationExpressionChanged(sender, e);
        }

        protected void txtRegex_OnServerValidate(object source, ServerValidateEventArgs args)
        {
            if (IsAdvancedValidation)
            {
                try
                {
                    new Regex(txtRegex.Text);
                } catch
                {
                    args.IsValid = false;
                }
            }
        }

        protected void ddlDataTypes_OnServerValidate(object source, ServerValidateEventArgs args)
        {
            Guid g;
            args.IsValid = string.IsNullOrEmpty(ddlValidation.SelectedValue) &&
                           ddlValidation.SelectedValue.IsGuidInline(out g);
        }

        /// <summary>
        /// IconPicker control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::TypeManagement.Controls.IconPicker IconPicker;  
    }
}
